package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dao.EventRepository;
import com.example.dao.YearRepository;
import com.example.entity.*;
import com.example.service.*;
import com.example.utils.AddRelation;
import com.example.utils.ConstantVariable;
import com.example.utils.FileUtil;
import com.example.utils.RemoveRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;


import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


import java.util.*;

/**
 *
 */
@Service
public class EventServiceImpl implements EventService {

    @Autowired
    EventRepository eventRepository;
    @Autowired
    YearService yearService;
    @Autowired
    YearRepository yearRepository;
    @Autowired
    MonthService monthService;
    @Autowired
    FileService fileService;
    @Autowired
    AddressService addressService;
    @Autowired
    KnowledgeElementService knowledgeElementService;
    @Autowired
    PeopleService peopleService;
    @Autowired
    FileNodeService fileNodeService;

    @Override
    public List<Event> selectAll() {
        return eventRepository.findAll();
    }

    @Override
    public Event selectOne(Long id) {
        Optional<Event> event_Optional = eventRepository.findById(id);
        Event event = event_Optional.get();
        return event;
    }

    @Override
    public JSONObject insert(Event e) {
        JSONObject jsonObject = new JSONObject();

        List<Long> addressIdList = e.getAddressIdList();
        List<Long> peopleIdList = e.getPeopleIdList();
        List<Long> knowledgeElementIdList = e.getKnowledgeElementIdList();
        Long mapId = e.getMapId();
        Long timeId = e.getTimeId();
        boolean hasChild = false;

        for (Long addressId:addressIdList) {
            Address addressNode = addressService.selectById(addressId);
            addressNode.setHasParent(true);
            GuanXi_EA guanXi_ea = new GuanXi_EA("LOCATION_IN", addressNode);
            e.getGuanXi_eaList().add(guanXi_ea);
            hasChild = true;
        }

        for (Long peopleId:peopleIdList) {
            People peopleNode = peopleService.selectById(peopleId);
            peopleNode.setHasParent(true);
            GuanXi_EP guanXi_ep = new GuanXi_EP("WHO", peopleNode);
            e.getGuanXi_epList().add(guanXi_ep);
            hasChild = true;
        }

        for (Long keId:knowledgeElementIdList) {
            KnowledgeElement knowledgeElementNode = knowledgeElementService.selectById(keId);
            knowledgeElementNode.setHasParent(true);
            GuanXi_EK guanXi_ek = new GuanXi_EK("KE", knowledgeElementNode);
            e.getGuanXi_ekList().add(guanXi_ek);
            hasChild = true;
        }
        if (mapId != null) {
            JSONObject jsonObject_Map = fileNodeService.selectOneById(mapId);
            FileNode mapFileNode = (FileNode) jsonObject_Map.get("data");
            mapFileNode.setHasParent(true);
            hasChild = true;
            e.getFileNodeList().add(mapFileNode);
        }

        if (timeId != null) {
            Month monthNode = monthService.selectById(timeId);
            monthNode.setHasParent(true);
            hasChild = true;
            GuanXi_EM guanXi_em = new GuanXi_EM("WHEN_IN", monthNode);
            e.getGuanXi_emList().add(guanXi_em);
        }

        e.setHasChild(hasChild);
        String uuid = UUID.randomUUID().toString();
        e.setLogic_id(uuid);
        Event eventNode = eventRepository.save(e);
        jsonObject.put("msg", "success");
        jsonObject.put("data", eventNode);
        return jsonObject;

    }





    @Override
    public JSONObject update(Event e) {
        JSONObject jsonObject = new JSONObject();
        Optional<Event> event_Optional = eventRepository.findById(e.getId());
        Event eventNode = event_Optional.get();
        eventNode.getGuanXi_ekList().clear();
        eventNode.getGuanXi_emList().clear();
        eventNode.getGuanXi_eaList().clear();
        eventNode.getGuanXi_epList().clear();
        eventNode.getPeopleIdList().clear();
        eventNode.getKnowledgeElementIdList().clear();
        eventNode.getAddressIdList().clear();
        eventNode.getFileNodeList().clear();
        eventNode.setMapId(null);
        eventNode.setTimeId(null);

        List<Long> peopleIdList = e.getPeopleIdList();
        List<Long> addressIdList = e.getAddressIdList();
        List<Long> knowledgeElementIdList = e.getKnowledgeElementIdList();
        Long mapId = e.getMapId();
        Long timeId = e.getTimeId();

        boolean curhasChild = false;


        for (Long addressId:addressIdList) {
            Address addressNode = addressService.selectById(addressId);
            addressNode.setHasParent(true);
            GuanXi_EA guanXi_ea = new GuanXi_EA("LOCATION_IN", addressNode);
            eventNode.getGuanXi_eaList().add(guanXi_ea);
            eventNode.getAddressIdList().add(addressId);
            curhasChild = true;
        }

        for (Long peopleId:peopleIdList) {
            People peopleNode = peopleService.selectById(peopleId);
            peopleNode.setHasParent(true);
            GuanXi_EP guanXi_ep = new GuanXi_EP("WHO", peopleNode);
            eventNode.getGuanXi_epList().add(guanXi_ep);
            eventNode.getPeopleIdList().add(peopleId);
            curhasChild = true;
        }

        for (Long keId:knowledgeElementIdList) {
            KnowledgeElement knowledgeElementNode = knowledgeElementService.selectById(keId);
            knowledgeElementNode.setHasParent(true);
            GuanXi_EK guanXi_ek = new GuanXi_EK("KE", knowledgeElementNode);
            eventNode.getGuanXi_ekList().add(guanXi_ek);
            eventNode.getKnowledgeElementIdList().add(keId);
            curhasChild = true;
        }
        if (mapId != null) {
            JSONObject jsonObject_Map = fileNodeService.selectOneById(mapId);
            FileNode mapFileNode = (FileNode) jsonObject_Map.get("data");
            mapFileNode.setHasParent(true);
            curhasChild = true;
            eventNode.getFileNodeList().add(mapFileNode);
            eventNode.setMapId(mapId);
        }

        if (timeId != null) {
            Month monthNode = monthService.selectById(timeId);
            monthNode.setHasParent(true);
            curhasChild = true;
            GuanXi_EM guanXi_em = new GuanXi_EM("WHEN_IN", monthNode);
            eventNode.getGuanXi_emList().add(guanXi_em);
            eventNode.setTimeId(timeId);
        }

        eventNode.setHasChild(curhasChild);
        eventNode.setEvent(e.getEvent());
        eventNode = eventRepository.save(eventNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", eventNode);
        return jsonObject;
    }



    @Override
    public JSONObject deleteById(Event e) {
        JSONObject jsonObject = new JSONObject();
        // 查找到该事件
        Optional<Event> event_Optional = eventRepository.findById(e.getId());
        Event eventNode = event_Optional.get();

        // 得到各个关系
        List<GuanXi_EM> guanXi_emList = eventNode.getGuanXi_emList();
        List<GuanXi_EA> guanXi_eaList = eventNode.getGuanXi_eaList();
        List<GuanXi_EK> guanXi_ekList = eventNode.getGuanXi_ekList();
        List<GuanXi_EP> guanXi_epList = eventNode.getGuanXi_epList();
        List<FileNode> fileNodeList = eventNode.getFileNodeList();
        if (guanXi_emList.size() == 0 && guanXi_eaList.size() == 0 && guanXi_ekList.size() == 0 && guanXi_epList.size() == 0 && fileNodeList.size() == 0) {
            eventRepository.deleteById(eventNode.getId());
            jsonObject.put("msg", "success");
            jsonObject.put("data", eventNode);
            return jsonObject;
        }
        jsonObject.put("msg", "删除失败，因为还有关联关系");
        return jsonObject;
    }


}
